package com.example.zhaojianxu.signin.securities.view

import android.os.Bundle
import android.view.View
import com.example.zhaojianxu.signin.common.constance.ComConstance
import com.example.zhaojianxu.signin.common.http.HttpUtil
import com.example.zhaojianxu.signin.common.util.ToastUtil
import kotlinx.android.synthetic.main.fragment_dialog_number.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 * create by @zhaojianxu on 2019/3/4
 */
class SellDialogFragment : NumberDialogFragment() {

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog_number_text.text = "请输入抛售数量"
        dialog_number_btn.text = "抛售"
        dialog_number_btn.setOnClickListener {
            sell()
        }
    }

    fun sell() {
        val sid = arguments?.getInt("sid")
        val bid = arguments?.getInt("bid")
        if (sid == null || bid == null) {
            ToastUtil.instance.showToast(this.activity,"服务器出错了～")
            dismiss()
        }
        val number = dialog_number_edit.text.toString().toInt()
        HttpUtil.instance.getSecuritiesApi()?.postSell(ComConstance.UID, sid!!, bid!!, number)
            ?.enqueue(object : Callback<String> {
                override fun onFailure(call: Call<String>, t: Throwable) {
                }

                override fun onResponse(call: Call<String>, response: Response<String>) {
                    if (response.isSuccessful) {
                        dismiss()
                    }
                }

            })
    }
}